package com.smfx.yxt.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.smfx.yxt.daoSqlserver.HkFaceGroupMapper;
import com.smfx.yxt.model.HkFaceGroup;
import com.smfx.yxt.service.IHkFaceGroupService;
import com.smfx.yxt.utils.HkFaceUtil;

@Service("hkFaceGroupService")
public class HkFaceGroupServiceImpl implements IHkFaceGroupService {

	@Autowired
	private HkFaceGroupMapper hkFaceGroupMapper;
	
	@Override
	public int insertSelective(HkFaceGroup record) {
		return hkFaceGroupMapper.insertSelective(record);
	}

	@Override
	public HkFaceGroup selectByPrimaryKey(Integer id) {
		return hkFaceGroupMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(HkFaceGroup record) {
		return hkFaceGroupMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public HkFaceGroup selectByIndexCode(String indexCode) {
		return hkFaceGroupMapper.selectByIndexCode(indexCode);
	}

	@Override
	public List<HkFaceGroup> selectListByGroupType(String groupType) {
		return hkFaceGroupMapper.selectListByGroupType(groupType);
	}

	@Override
	public HkFaceGroup selectByGroupName(String groupType, String grade) {
		HkFaceGroup faceGroup = hkFaceGroupMapper.selectByGroupName(groupType, grade);
		if(faceGroup == null) {
			String name = "";
			if("parent".equals(groupType)) {
				name = grade + "家长组";
			}else if("student".equals(groupType)) {
				name = grade + "学生组";
			}else {
				name = "教师组";
			}
			String result = HkFaceUtil.addSingleFaceGroup(name, name);
			JSONObject jsonObject = JSONObject.parseObject(result);
			JSONObject data = jsonObject.getJSONObject("data");
			faceGroup = new HkFaceGroup();
			faceGroup.setIndexCode(data.getString("indexCode"));
			faceGroup.setGroupType(groupType);
			faceGroup.setName(name);
			faceGroup.setDescription(name);
			hkFaceGroupMapper.insertSelective(faceGroup);
		}
		return faceGroup;
	}

}
